package com.yunpuvip.sachet.modular.system.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.IdUtil;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.yunpuvip.sachet.config.properties.GunsProperties;
import com.yunpuvip.sachet.modular.constant.ApplicationConstant;
import com.yunpuvip.sachet.modular.core.exception.RestServiceExceptionEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.*;

/**
 * @ClassName UploadController
 * @Description TODO
 * @Author Lincoln
 * @Date 2018/7/26
 * @Version 1.0
 */
@Controller
@RequestMapping("/upload")
@Slf4j
public class UploadController {

    @Autowired
    private GunsProperties gunsProperties;

   /* @Autowired
    UploadService uploadService;
    @Autowired
    private SysparamService sysparamService;*/
/*

    */



    /**
     * 上传图片 for wangEditor
     *//*
    @RequestMapping(method = RequestMethod.POST, path = "/wang")
    @ResponseBody
    public Map uploadForWangEditor(@RequestPart("file") MultipartFile picture, HttpServletRequest request) {
        String fileUploadPath = gunsProperties.getFileUploadPath();
        //String adminName = gunsProperties.getAdminName();
        String adminName= request.getContextPath();
        String imgPath = uploadService.uploadSingleImage(fileUploadPath, picture);
        Map<String, Object> resultMap = new HashMap<>() ;
        resultMap.put("errno", 0);
        List<String> list = new ArrayList();
//        http://twmall.yunpuvip.com/tewei-admin/img/20180815/eee00dabcc024ebc8c504f0fe647f80b. jpg
        Sysparam sysparam = sysparamService.loadSetting(ApplicationConstant.WEBHOST);
        imgPath = sysparam.getParamValue() + adminName + imgPath;
        System.out.println("imgPath: " + imgPath);
        list.add(imgPath);
        resultMap.put("data", list);
        return resultMap;
    }*/



    /**
     * 上传视频
     * @param video
     * @return
     */
    @RequestMapping(method = RequestMethod.POST, path = "/video")
    @ResponseBody

    public ResponseData uploadVideo(@RequestPart("file") MultipartFile video) {
        String suffix = ToolUtil.getFileSuffix(video.getOriginalFilename());
        String s = suffix.toLowerCase();
        //判断文件后缀，防止被黑0101101110101010110
        log.info("开始上传文件");
        String[] imgSuffix = {"avi", "mp4", "rmvb", "flv"};
        if (!ArrayUtil.contains(imgSuffix, s)) {
            return ResponseData.error(RestServiceExceptionEnum.FILE_SUFFIX_ERROR.getCode(), RestServiceExceptionEnum.FILE_SUFFIX_ERROR.getMessage());
        }
        String relativePath="";
        log.info("获取文件上传路径");
        try{
            relativePath = getfinalFileName(video, gunsProperties.getFileUploadPath());
        } catch (Exception e) {
            return ResponseData.error(RestServiceExceptionEnum.FILE_UPLOAD_ERROR.getCode(), RestServiceExceptionEnum.FILE_UPLOAD_ERROR.getMessage());
        }
        log.info("上传成功!!!");
        //返回前端一个图片的URL，带上img，做nginx的路由配置
        return ResponseData.success(relativePath);
    }

    /**
     * 处理文件
     * @param uploadFile
     * @param uploadPath 上传路径
     * @return
     * @throws IOException
     */
    private String getfinalFileName(MultipartFile uploadFile,String uploadPath ) throws IOException {
        String fileName = IdUtil.simpleUUID() + "." + ToolUtil.getFileSuffix(uploadFile.getOriginalFilename());
        //文件夹加上日期
        String today = DateUtil.format(new Date(), "yyyyMMdd");
        String relativePath= today+"/";
        String fileSavePath = uploadPath +relativePath;
        File file = new File(fileSavePath);
        if (!file.exists()) {
            file.mkdirs();
        }
        String fullPath = fileSavePath +fileName;
        log.debug("upload video fullPath: {}", fullPath);
        uploadFile.transferTo(new File(fullPath));
        String reLoad = fullPath.substring(fullPath.lastIndexOf("img"));
        return  reLoad ;
    }

    public static void main(String[] args) {
        String uploadPath = "asdfasdfa/MP45";
        //System.out.println(uploadPath.substring(uploadPath.lastIndexOf("img")) );
        System.out.println(uploadPath.toLowerCase() );

    }

}
